package com.cloud.pole.pay.common.ccbpay;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.druid.util.StringUtils;
import com.cloud.pole.common.util.security.MD5Utils;

//import CCBSign.RSASig;

public class CcbPayConfig {
//	public static final String MERCHANTID = "105000148128939"; // 商户代码
//	public static final String POSID = "031040950"; // 商户柜台代码
//	public static final String BRANCHID = "430000000"; // 分行代码
//	public static final String CURCODE = "01"; // 币种
//	public static final String TXCODE = "520100"; // 交易码
//	public static final String TYPE = "1"; // 接口类型
//	public static final String PUB = "9ddadfd749f06efd3ff022d3020111";
//	public static final String PUBKEY = "30819d300d06092a864886f70d010101050003818b00308187028181009b120cf1fe88c526191e3b0709fbac11087aa5c89936ce79425c8f4c667781b41be87316538a7e73cf36ccf96df95c8e9e17a672f5c71dc4e53b7c74d3b65eb30ebb210afd1499f9666fb4bc928a4c692b509362a6be9b3e30f158fe86d679bec2e7e5706ceb46d2a8f8b2d813d3bcc539cc1a7d9ddadfd749f06efd3ff022d3020111";


	/**
	 * 金蜂星建行分期参数
	 */
	public static final String MERCHANTID = "105000257322934"; // 商户代码
	public static final String POSID = "042114723"; // 商户柜台代码
	public static final String BRANCHID = "430000000"; // 分行代码
    public static final String CURCODE = "01"; // 币种
	public static final String TXCODE = "520100"; // 交易码
	public static final String TYPE = "1"; // 接口类型
	public static final String PUB = "a95b7093b7e30a07dced4f53020111";
	public static final String PUBKEY = "30819c300d06092a864886f70d010101050003818a00308186028180778f79be0e88f7d0a54c969982e0c4be0be121506d3e383421e1421a950c94410438ff92d6c035bbaab31af430a3c48b607a96e8cda49c10fcf1724dc66996981621f618e81bf7c3693657e25a719ee7de1441ab6c70bdd848593d07bde4361f6951bab7111ed1f76a9a5c46f99b4786052e892ea95b7093b7e30a07dced4f53020111";
	// public static final String BANKURL =
	// "http://128.128.96.175:8001/app/ccbMain";
	public static final String BANKURL = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain";

	public static String CcbMd5(String orderno, String paymoney) {
		try {
			StringBuffer tmp = new StringBuffer();
			String REMARK1 = "预存话费办保底送建行智能ETC后视镜";
			tmp.append("MERCHANTID=").append(CcbPayConfig.MERCHANTID).append("&POSID=").append(CcbPayConfig.POSID)
					.append("&BRANCHID=").append(CcbPayConfig.BRANCHID).append("&ORDERID=").append(orderno)
					.append("&PAYMENT=").append(paymoney).append("&CURCODE=").append(CcbPayConfig.CURCODE)
					.append("&TXCODE=").append(CcbPayConfig.TXCODE).append("&REMARK1=").append("").append("&REMARK2=")
					.append("").append("&TYPE=").append(TYPE).append("&PUB=").append("").append("&GATEWAY=").append("")
					.append("&CLIENTIP=").append("").append("&REGINFO=").append("").append("&PROINFO=").append("")
					.append("&REFERER=").append("").append("&INSTALLNUM=").append("3");
			String queryStr = MD5Utils.md5Hex(tmp.toString());
			return queryStr;
			// reqData.put("MAC",queryStr);
			// reqData.put("PUB","");
			// reqData.put("GATEWAY","");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public static String CcbPayGetUrl(String orderno, String paymoney) {
		try {
			StringBuffer tmp = new StringBuffer();
			String REMARK1 = "预存话费办保底送建行智能ETC后视镜";
			tmp.append("MERCHANTID=").append(CcbPayConfig.MERCHANTID).append("&POSID=").append(CcbPayConfig.POSID)
					.append("&BRANCHID=").append(CcbPayConfig.BRANCHID).append("&ORDERID=").append(orderno)
					.append("&PAYMENT=").append(paymoney).append("&CURCODE=").append(CcbPayConfig.CURCODE)
					.append("&TXCODE=").append(CcbPayConfig.TXCODE).append("&REMARK1=").append("").append("&REMARK2=")
					.append("").append("&TYPE=").append(TYPE).append("&PUB=").append(PUB).append("&GATEWAY=").append("")
					.append("&CLIENTIP=").append("").append("&REGINFO=").append("").append("&PROINFO=").append("")
					.append("&REFERER=").append("");

			String url = CcbPayConfig.BANKURL + "?" + tmp + "&MAC=" + MD5Utils.md5Hex(tmp.toString());
			return url;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	

	/**
	 * 信用卡
	 * @param orderno
	 * @param paymoney
	 * @return
	 */
	public static String CcbPayGetUrlCredit(String orderno, String paymoney) {
		try {
			StringBuffer tmp = new StringBuffer();
			String REMARK1 = "预存话费办保底送建行智能ETC后视镜";
			tmp.append("MERCHANTID=").append(CcbPayConfig.MERCHANTID).append("&POSID=").append(CcbPayConfig.POSID)
					.append("&BRANCHID=").append(CcbPayConfig.BRANCHID).append("&ORDERID=").append(orderno)
					.append("&PAYMENT=").append(paymoney).append("&CURCODE=").append(CcbPayConfig.CURCODE)
					.append("&TXCODE=").append(CcbPayConfig.TXCODE).append("&REMARK1=").append("").append("&REMARK2=")
					.append("").append("&TYPE=").append(TYPE).append("&PUB=").append(PUB).append("&GATEWAY=").append("")
					.append("&CLIENTIP=").append("").append("&REGINFO=").append("").append("&PROINFO=").append("")
					.append("&REFERER=").append("");

			String url = CcbPayConfig.BANKURL + "?" + tmp + "&MAC=" + MD5Utils.md5Hex(tmp.toString());
			return url;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public static String CcbAppPayGetInfo(String orderno, String paymoney) {
		try {
			StringBuffer tmp = new StringBuffer();
			String REMARK1 = "预存话费办保底送建行智能ETC后视镜";
			tmp.append("MERCHANTID=").append(CcbPayConfig.MERCHANTID).append("&POSID=").append(CcbPayConfig.POSID)
					.append("&BRANCHID=").append(CcbPayConfig.BRANCHID).append("&ORDERID=").append(orderno)
					.append("&PAYMENT=").append(paymoney).append("&CURCODE=").append(CcbPayConfig.CURCODE)
					.append("&TXCODE=").append(CcbPayConfig.TXCODE).append("&REMARK1=").append("").append("&REMARK2=")
					.append("").append("&TYPE=").append(TYPE).append("&PUB=").append(PUB).append("&GATEWAY=").append("")
					.append("&CLIENTIP=").append("").append("&REGINFO=").append("").append("&PROINFO=").append("")
					.append("&REFERER=").append("");

			String info = tmp.toString() + "&MAC=" + MD5Utils.md5Hex(tmp.toString());
			return info;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 生成签名源串
	 * 验证订单支付通过结果
	 * @param orderno
	 * @param paymoney
	 * @return
	 */
	public static String CcbPayNotifySrc(String orderno, String paymoney, String remark1, String remark2,
			String acctype, String referer, String clientip, String accdate,String usrmsg,String errmsg) {
		try {
			// &SUCCESS=Y&TYPE=1&REFERER=http://www.ccb.com/index.jsp&CLIENTIP=172.0.0.1&ACCDATE=20100907
			// &USRMSG=T4NJx%2FVgocRsLyQnrMZLyuQQkFzMAxQjdqyzf6pM%2Fcg%3D&INSTALLNUM=3&ERRMSG=
			StringBuffer tmp = new StringBuffer();
			tmp.append("POSID=").append(CcbPayConfig.POSID).append("&BRANCHID=").append(CcbPayConfig.BRANCHID)
					.append("&ORDERID=").append(orderno).append("&PAYMENT=").append(paymoney)
					.append("&CURCODE=").append(CcbPayConfig.CURCODE).append("&REMARK1=").append(remark1)
					.append("&REMARK2=").append(remark2).append("&ACC_TYPE=").append(acctype).append("&SUCCESS=").append("Y");
			//tmp.append("&TYPE=").append("1").append("&REFERER=").append(referer).append("&CLIENTIP=").append(clientip);
			if(!StringUtils.isEmpty(accdate)) {
				tmp.append("&ACCDATE=").append(accdate);
			}
			if(!StringUtils.isEmpty(usrmsg)) {
				tmp.append("&USRMSG=").append(usrmsg);
			}
			if(!StringUtils.isEmpty(errmsg)) {
				tmp.append("&ERRMSG=").append(errmsg);
			}
			
			return tmp.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";

	}
	
	public static String CcbPayNotifySrc(HttpServletRequest request) {
		try {
			
			String orderno = request.getParameter("ORDERID");
			String paymoney = request.getParameter("PAYMENT");
			String remark1 = request.getParameter("REMARK1");
			String remark2 = request.getParameter("REMARK2");
			String acctype = request.getParameter("ACC_TYPE");
			String accdate = request.getParameter("ACCDATE");
			String usrmsg = request.getParameter("USRMSG");
			String errmsg = request.getParameter("ERRMSG");
			
			// &SUCCESS=Y&TYPE=1&REFERER=http://www.ccb.com/index.jsp&CLIENTIP=172.0.0.1&ACCDATE=20100907
			// &USRMSG=T4NJx%2FVgocRsLyQnrMZLyuQQkFzMAxQjdqyzf6pM%2Fcg%3D&INSTALLNUM=3&ERRMSG=
			StringBuffer tmp = new StringBuffer();
			tmp.append("POSID=").append(CcbPayConfig.POSID).append("&BRANCHID=").append(CcbPayConfig.BRANCHID)
					.append("&ORDERID=").append(orderno).append("&PAYMENT=").append(paymoney)
					.append("&CURCODE=").append(CcbPayConfig.CURCODE).append("&REMARK1=").append(remark1)
					.append("&REMARK2=").append(remark2).append("&ACC_TYPE=").append(acctype).append("&SUCCESS=").append("Y");
			//tmp.append("&TYPE=").append("1").append("&REFERER=").append(referer).append("&CLIENTIP=").append(clientip);
			if(!StringUtils.isEmpty(accdate)) {
				tmp.append("&ACCDATE=").append(accdate);
			}
			if(!StringUtils.isEmpty(usrmsg)) {
				tmp.append("&USRMSG=").append(usrmsg);
			}
			if(!StringUtils.isEmpty(errmsg)) {
				tmp.append("&ERRMSG=").append(errmsg);
			}
			
			return tmp.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public static void main(String[] args) {		
		
		String url = CcbPayGetUrl(Long.toString(System.currentTimeMillis()), "5001");
		System.out.println(url);

	}
}
